package com.campus.web.controller.study;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.campus.app.service.CounsellorsService;
import com.campus.app.service.StudentsService;
import com.campus.commons.entity.Class;
import com.campus.commons.entity.ClassRoom;
import com.campus.commons.entity.Counsellor;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.Result;
import com.campus.web.controller.management.ClassRoomController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 助教团辅导班级表 前端控制器
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@RestController
@RequestMapping("/counsellor")
public class CounsellorController {

    @Autowired
    private CounsellorsService counsellorsService;
    @Autowired
    private StudentsService studentsService;


    @GetMapping("/")
    public ModelAndView toCheckEvening(ModelMap modelMap) {
        modelMap.addAttribute("class", Class.classes.values());
        modelMap.addAttribute("classroom", ClassRoomController.getClassRooms());
        modelMap.addAttribute("students", studentsService.getStudentByDeId(1).getData());
        return new ModelAndView("learn/counsellor/index",modelMap);
    }

    //<editor-fold desc="小黑屋">
    @GetMapping("/edit")
    public ModelAndView toEdit(int id) {
        ModelAndView modelAndView = Edit();
        modelAndView.addObject("edit", true);
        modelAndView.addObject("info",counsellorsService.getById(id));
        return modelAndView;
    }
    private ModelAndView Edit() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("class", Class.classes.values());
        List<HashMap<String, Object>> classrooms = ClassRoom.classrooms.keySet().stream().map(s -> {
            HashMap<String, Object> sr = new HashMap<>();
            sr.put("classroomName", ClassRoom.classrooms.get(s));
            sr.put("id", s);
            return sr;
        }).collect(Collectors.toList());
        modelAndView.addObject("classroom",classrooms );
        modelAndView.addObject("students", studentsService.getStudentByDeId(1).getData());
        modelAndView.setViewName("learn/counsellor/edit");
        return modelAndView;
    }
    @GetMapping("/add")
    public ModelAndView toAdd() {
        ModelAndView edit = Edit();
        edit.addObject("edit", false);
        return edit;
    }
    //</editor-fold>



    //    获取所有
    @PostMapping("/all")
    public String getAllCounsellor(int page,int limit) {
        return JSON.toJSONString(counsellorsService.getAllCounsellor(page, limit), SerializerFeature.WriteDateUseDateFormat);
    }

    //    条件
    @PostMapping("/search")
    public String getCounsellors(int page, int limit , Date start,Date end,String className) {
        return JSON.toJSONString(counsellorsService.getCounsellors(page, limit, start, end, className),SerializerFeature.WriteDateUseDateFormat);
    }

    //    添加
    @PostMapping("/add")
    public String addCounsellor(Counsellor counsellor) {
        return JSON.toJSONString(counsellorsService.addCounsellor(counsellor));
    }

    //    删除
    @PostMapping("/del")
    public String delCounsellor(int id) {
        return JSON.toJSONString(counsellorsService.delCounsellor(id));
    }

    //    更新
    @PostMapping("/update")
    public String upCounsellor(Counsellor counsellor,String studentName,String classroomName,String className) {
        counsellor.setClassName(className);
        counsellor.setClassroomName(classroomName);
        counsellor.setStudentName(studentName);
        return JSON.toJSONString(counsellorsService.upCounsellorVerify(counsellor));
    }


    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        if (file!=null) {
            try {
               return counsellorsService.upLoadCounsellors(file.getInputStream(), file.getOriginalFilename());
            } catch (IOException e) {
                e.printStackTrace();
                return Result.error(Error.File_E1R, "读取异常");
            }
        }
        return null;
    }


}

